class WarehousesController < ApplicationController
  # GET /warehouses
  # GET /warehouses.xml
  
  before_filter :login_required
  before_filter :session_expiry
  
  private
  def set_positions_from_cookie
    position_strings = cookies[:positions].split(',')
	position_strings.each do |position_string|
	  position_data = position_string.split('_')
      new_position = WarehousePosition.new
	  new_position.warehouse_id = @warehouse.id
	  new_position.product_id = position_data[0]
	  new_position.quantity = position_data[1]
	  @warehouse.positions << new_position
	end
  end
  
  public
  def index
    @warehouses = Warehouse.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @warehouses }
    end
  end

  # GET /warehouses/1
  # GET /warehouses/1.xml
  def show
    @warehouse = Warehouse.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @warehouse }
    end
  end

  # GET /warehouses/new
  # GET /warehouses/new.xml
  def new
    @warehouse = Warehouse.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @warehouse }
    end
  end

  # GET /warehouses/1/edit
  def edit
    @warehouse = Warehouse.find(params[:id])
  end

  # POST /warehouses
  # POST /warehouses.xml
  def create
    @warehouse = Warehouse.new(params[:warehouse])
	
	set_positions_from_cookie

    respond_to do |format|
      if @warehouse.save
        flash[:notice] = 'Warehouse was successfully created.'
        format.html { redirect_to(@warehouse) }
        format.xml  { render :xml => @warehouse, :status => :created, :location => @warehouse }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @warehouse.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /warehouses/1
  # PUT /warehouses/1.xml
  def update
    @warehouse = Warehouse.find(params[:id])
	
	@warehouse.positions = []
	set_positions_from_cookie

    respond_to do |format|
      if @warehouse.update_attributes(params[:warehouse])
        flash[:notice] = 'Warehouse was successfully updated.'
        format.html { redirect_to(@warehouse) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @warehouse.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /warehouses/1
  # DELETE /warehouses/1.xml
  def destroy
    @warehouse = Warehouse.find(params[:id])
    @warehouse.destroy

    respond_to do |format|
      format.html { redirect_to(warehouses_url) }
      format.xml  { head :ok }
    end
  end
  
  def filter
    @query_name = "%"+params[:filter][:warehouse_name]+"%"
    @query_street = "%"+params[:filter][:warehouse_street]+"%"
    @query_city = "%"+params[:filter][:warehouse_city]+"%"
    @query_postal_code = "%"+params[:filter][:warehouse_postal_code]+"%"
    @query_country = "%"+params[:filter][:warehouse_country]+"%"
    @query_area = params[:filter][:warehouse_area]
    
    @warehouses = Warehouse.find(:all, :conditions => ["name like ? AND street like ? AND city like ? AND postal_code like ? AND country like ? AND (area = ?  OR 'ignore' = ?)", 
                                                    @query_name, @query_street, @query_city, @query_postal_code, @query_country, @query_area, @query_area])
  end
end
